package com.useeinfo.oa.modules.base.biz;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.func.file.FilePathConfig;
import com.useeinfo.framework.sugar.func.file.FileUtils;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.config.ConstantKeyFilePath;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.dao.ApplicationModuleDao;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.enums.ApplicationTypeEnum;
import com.useeinfo.oa.modules.security.biz.SystemModuleBiz;
import com.useeinfo.oa.modules.security.entity.SystemModule;
import com.useeinfo.oa.modules.security.vo.TreeVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-04
 * @createTime: 16:12:33
 * 应用模块service
 */
@Service
public class ApplicationModuleBiz extends CrudBiz<ApplicationModuleDao, ApplicationModule> {

    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private SystemModuleBiz systemModuleBiz;

    /**
     * 应用模块编辑保存
     *
     * @param applicationModule
     * @param type
     * @param file
     * @return
     */
    public ResultDto saveOrupdate(ApplicationModule applicationModule, String type, MultipartFile file) {
        String path = "";
        if (null != file && !file.isEmpty()) { //判断图片
             path = FileUtils.fileUpload(file, ConstantKeyFilePath.FILE_UPLOAD_ROOT_PATH, FilePathConfig.APPLIANCE_PHOTO_DIR,Configuration.getConfigurationByName("fileSaveRootPath"));
            if (!StringConverters.null2String(path).equals(""))
                applicationModule.setIcon(path);
        }
        if (null == applicationModule.getId()) {
            applicationModule.setCreateDate(new Date());
            applicationModule.setApplicationTypeEnum(ApplicationTypeEnum.valueOf(type));
            dao.add(applicationModule);
            systemModuleBiz.addOrUpdate(new SystemModule(applicationModule.getApplicationName()
                    , applicationModule.getApplicationName(), applicationModule.getId()));
        } else {
            ApplicationModule application = dao.findModel(applicationModule.getId());
            application.setApplicationTypeEnum(ApplicationTypeEnum.valueOf(type));
            if(!StringConverters.null2String(path).equals("")) {
                application.setIcon(path);
            }
            application.setApplicationName(applicationModule.getApplicationName());
            application.setUrl(applicationModule.getUrl());
            dao.update(application);
            SystemModule systemModule = systemModuleBiz.findSystemModuleByapplicationId(applicationModule.getId() + "");
            if(null == systemModule){
                systemModule = new SystemModule();
                systemModule.setApplicationModuleId(applicationModule.getId());
            }
            systemModule.setModuleName(applicationModule.getApplicationName());
            systemModule.setModuleDesc(applicationModule.getApplicationName());
            systemModuleBiz.addOrUpdate(systemModule);
        }
        return ResultUtil.success();
    }

    /**
     * 获取所有权限数据
     *
     * @return
     */
    public ResultDto getApplicationTree(String sysCode, String enterpriseId) {
        JSONObject data = new JSONObject();
        List<TreeVO> treeVOList = new ArrayList<>();
        List<ApplicationModule> applicationModules = this.findList();
        if (CollectionUtils.isNotEmpty(applicationModules)) {
            applicationModules.forEach(applicationModule -> treeVOList.add(new TreeVO(applicationModule.getApplicationName(), "0",
                    applicationModule.getId() + "", applicationModule.getApplicationName(), "module")));
        }

        JSONObject menuMap = new JSONObject();
        menuMap.put(sysCode, treeVOList);
        data.put("menuMap", menuMap);

        //选中项
        data.put("roleMenuList", enterpriseBiz.getApplication(sysCode, StringConverters.ToLong(enterpriseId)));
        return ResultUtil.success(data);
    }

    public List<ApplicationModule> hqlExcute(String hql){
        return dao.hqlExcute(hql);
    }

}
